In [12]:
import tensorflow as tf
import numpy as np

In [2]:
a=tf.constant(5)
b=tf.constant(5)
c=a*b

with tf.Session() as sess:
	print(sess.run(c))
	x=c.eval()
	print(c.eval())


print(x)


25
25
25

In [4]:
W1 = tf.ones((2,2))
W2 = tf.Variable(tf.zeros((2,2)), name="weights")
with tf.Session() as sess:

    sess.run(tf.initialize_all_variables())
    print(sess.run(W2))
    print(sess.run(W1))


[[ 0.  0.]
 [ 0.  0.]]
[[ 1.  1.]
 [ 1.  1.]]

In [7]:
#### Updating variable
state = tf.Variable(0, name="counter")
new_value = tf.add(state, tf.constant(1))
update = tf.assign(state, new_value)

with tf.Session() as sess:
	sess.run(tf.initialize_all_variables())
	print(sess.run(state))
	for _ in range(3):
         sess.run(update)
         print(sess.run(state))


0
1
2
3

In [10]:
###Fetching Variable State (1)
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
intermed = tf.add(input2, input3)
mul = tf.mul(input1, intermed)

with tf.Session() as sess:
	result = sess.run([mul, intermed])
	print("result is      :"+str(result))


result is      :[21.0, 7.0]

In [14]:
### Convert numpy to tflow
##Inputting Data 
a = np.zeros((3,3))
ta = tf.convert_to_tensor(a)
with tf.Session() as sess:
	print(sess.run(ta))


[[ 0.  0.  0.]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]

In [16]:
### Placeholders and Feed Dictionaries (2)
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.mul(input1, input2)

In [19]:
#### Placeholders and Feed Dictionaries (2)
# pass values to inputs uing feed dictionary
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.mul(input1, input2)
with tf.Session() as sess:
	print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))


[array([ 14.], dtype=float32)]

In [20]:
#### Placeholders and Feed Dictionaries (2)
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.mul(input1, input2)
with tf.Session() as sess:
	print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))


[array([ 14.], dtype=float32)]

In [ ]:
### Scope of variables
with tf.variable_scope("foo"):
 with tf.variable_scope("bar"):
    v = tf.get_variable("v", [1])

#            assert v.name == "foo/bar/v:0



with tf.variable_scope("foo"):
 v = tf.get_variable("v", [1])
 tf.get_variable_scope().reuse_variables()
 v1 = tf.get_variable("v", [1])
assert v1 == v

Ex: Linear Regression in TensorFlow (1)


In [45]:
import numpy as np
import seaborn
import matplotlib.pyplot as plt
% matplotlib inline
# Define input data
X_data = np.arange(100, step=.1)
y_data = X_data + 20 * np.sin(X_data/10)
# Plot input data
plt.scatter(X_data, y_data)


Out[45]:
<matplotlib.collections.PathCollection at 0x7f447bddcb38>

In [46]:
# Define data size and batch size
n_samples = 1000
batch_size = 1000
# Tensorflow is finicky about shapes, so resize
X_data = np.reshape(X_data, (n_samples,1))
y_data = np.reshape(y_data, (n_samples,1))
# Define placeholders for input
X = tf.placeholder(tf.float32, shape=(batch_size, 1))
y = tf.placeholder(tf.float32, shape=(batch_size, 1))

Define variables to be learned